package com.example.himalaya2.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.himalaya2.R;
import com.example.himalaya2.base.BaseApplication;
import com.example.himalaya2.utils.LogUtil;

import org.jetbrains.annotations.NotNull;

public abstract class UILoader extends FrameLayout {
    private static final String TAG ="UILoader";  // FrameLayout 比较干净

    public View mLoadingView;
    private View mSuccessView;
    private View mNetworkErrorView;
    private View mEmptyView;
    private OnRetryClickListener mOnRetryClickListener = null;

    public enum UIStatus{
        LOADING,SUCCESS,NETWORK_ERROR,EMPTY,NONE
    }

    public UIStatus mCurrentStatus = UIStatus.NONE;

    public UILoader(@NonNull @NotNull Context context) {
        this(context,null);
    }

    public UILoader(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UILoader(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void updateStatus(UIStatus status) {
        mCurrentStatus = status;
        // 更新ui一定要在主线程
        BaseApplication.getsHandler().post(new Runnable() {  // handler.post(r)。是要执行的任务代码。意思就是说r的代码实际是在UI线程执行的。可以写更新UI的代码
            @Override
            public void run() {
                switchUIByCurrentStatus();
            }
        });

    }


    /*
    * 初始化UI
    * */
    private void init() {
        switchUIByCurrentStatus();
    }

    private void switchUIByCurrentStatus() {
        // 加载中
        if (mLoadingView == null) {
            mLoadingView = getLoadingView();
            addView(mLoadingView);  // 给该布局 添加控件
        }
        // 根据状态设置是否可见
        mLoadingView.setVisibility(mCurrentStatus == UIStatus.LOADING?VISIBLE:GONE);  // 该控件是否显示

        // 成功
        if (mSuccessView == null) {
            mSuccessView = getSuccessView(this);
            LogUtil.d(TAG,"BEFORE SUCCESS addView()");
            addView(mSuccessView);  // TODO
            LogUtil.d(TAG,"AFTER SUCCESS addView()");
        }
        // 根据状态设置是否可见
        mSuccessView.setVisibility(mCurrentStatus == UIStatus.SUCCESS?VISIBLE:GONE);

        // 网络错误页面
        if (mNetworkErrorView == null) {
            mNetworkErrorView = getNetworkErrorView();
            addView(mNetworkErrorView);
        }
        // 根据状态设置是否可见
        mNetworkErrorView.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR?VISIBLE:GONE); // TODO

        // 数据为空
        if (mEmptyView == null) {
            mEmptyView = getEmptyView();
            addView(mEmptyView);
        }
        // 根据状态设置是否可见
        mEmptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY?VISIBLE:GONE);
    }

    private View getEmptyView(){
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view,this,false);
    }


    private View getNetworkErrorView(){
        View networkErrorView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_error_view,this,false);
        networkErrorView.findViewById(R.id.network_error_icon).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO:去重新获取数据
                if (mOnRetryClickListener !=null) {
                    mOnRetryClickListener.onRetryClick();
                }
            }
        });

        return networkErrorView;
    }

    protected abstract View getSuccessView(ViewGroup container);


    private View getLoadingView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading_view,this,false);
    }

    public void setOnRetryClickListener(OnRetryClickListener listener) {
        this.mOnRetryClickListener = listener;
    }

    public interface OnRetryClickListener {
        void onRetryClick();
    }

}
